package com.itheima.reggie.controller;



import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.commen.R;
import com.itheima.reggie.domain.Category;

import com.itheima.reggie.service.ICategoryService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpServletRequest;
import java.util.List;


@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private ICategoryService service;

/*    @Autowired
    private IDishService dishService;

    @Autowired
    private ISetmealService setmealService;*/

    //新增分类
    @PostMapping
    public R<String> save(@RequestBody Category category, HttpServletRequest request) {
        Long employeeId = (Long) request.getSession().getAttribute("employee");

       /* category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        category.setCreateUser(employeeId);
        category.setUpdateUser(employeeId);*/
        service.save(category);
        return R.success("新增分类成功");
    }

    //分类分页查询
    @GetMapping("/page")
    public R page(int page, int pageSize) {

        LambdaQueryWrapper<Category> qw = new LambdaQueryWrapper<>();
        qw.orderByAsc(Category::getSort);
        IPage<Category> page1 = new Page<>(page, pageSize);
        service.page(page1, qw);
        return R.success(page1);


    }

    //删除分类
    @DeleteMapping
    public R deleteById(Long id) {
    /*    1、检查当前分类下是否有对应的菜品
        SELECT COUNT ( *)FROM `dish`WHERE category_id = 1397844263642378242
        结果大于0 ，则不能删除 代码结束 -- > 自定义一个业务异常，然后抛出 throw new BusinessException("该分类下有菜品，不能删除");

        2、检查当前分类下是否有对应的套餐
        SELECT COUNT ( *)FROM `setmeal`WHERE category_id = 1397844263642378242
        结果大于0 ，则不能删除 代码结束 -- > 自定义一个业务异常，然后抛出 throw new BusinessException("该分类下有套餐，不能删除");

        3、可以删除, 则调用mapper直接删除 categoryMapper.deleteById(id);*/

/*        LambdaQueryWrapper<Dish> qw = new LambdaQueryWrapper<>();
        qw.eq(Dish::getCategoryId,id);
        int count = dishService.count();
        if (count>0){
            return R.error("不能删除");
        }

        LambdaQueryWrapper<Setmeal> qw1 = new LambdaQueryWrapper<>();
        qw1.eq(Setmeal::getCategoryId,id);
        int count1 = setmealService.count(qw1);
        if (count1>0){
            return R.error("不能删除");
        }*/
        service.remove(id);
        return R.success("分类信息删除成功");


    }

    //修改分类
    @PutMapping
    public R update(@RequestBody Category category) {
        service.updateById(category);
        return R.success("修改成功");
    }

    //查询菜品分类列表数据
    @GetMapping("/list")
    public R list(Category category) {
        Integer type = category.getType();
        LambdaQueryWrapper<Category> qw = new LambdaQueryWrapper<>();
        qw.eq(type != null, Category::getType, type).orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);
        List<Category> list = service.list(qw);
        return R.success(list);
    }


}
